<?php

namespace App\Admin\Controllers;

use App\Models\LogisticsCompany;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;

class LogisticsCompanysController extends Controller
{
    use HasResourceActions;

    //首页
    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header('物流公司管理');
            $content->body($this->grid());
        });
    }

    //编辑物流公司
    public function edit($id, Content $content)
    {
        return $content->header('物流公司')->description('编辑')->body($this->form()->edit($id));
    }

    //编辑物流公司
    public function update($id)
    {
        return $this->form()->update($id);
    }

    //添加物流公司
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('创建物流公司');
            $content->body($this->form());
        });
    }


    //添加文章保存回调
    public function store()
    {
        return $this->form()->store();
    }

    //获取物流公司
    protected function grid()
    {
        return Admin::grid(LogisticsCompany::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->name('物流公司名称')->style('max-width:200px;word-break:break-all;');
            $grid->create_at('创建时间');
            $grid->is_show('是否显示')->display(function ($value) {
                return ($value==1) ? '是' : '否';
            });

            $grid->actions(function ($actions) {
                $actions->disableView();
            });

            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();

                // 在这里添加字段过滤器
                $filter->column(1/2, function ($filter) {
                    $filter->like('name', '物流公司名称');
                });
            });


            $grid->model()->orderBy('id', 'desc');

            //禁用导出按钮
            $grid->disableExport();
        });
    }

    //修改表单事项
    protected function form()
    {
        // 创建一个表单
        return Admin::form(LogisticsCompany::class, function (Form $form) {
            $form->text('name', '物流公司名称')->attribute(['maxlength' => '50'])->rules('required|max:50');
            $form->radio('is_show', '是否显示')->options([1 => '是', 0 => '否'])->default(1);

            // 定义事件回调，当模型即将保存时会触发这个回调
            $form->saving(function (Form $form) {
            });
            $form->tools(function (Form\Tools $tools) {
                // 去掉`删除`按钮
                $tools->disableDelete();
            });
        });
    }
}
